// DocumentHomeWebPage.cpp: implementation of the CDocumentHomeWebPage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AUSlotsTester.h"
#include "DocumentHomeWebPage.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDocumentHomeWebPage::CDocumentHomeWebPage( const CAUSlotsTesterDoc* pDoc ) :
	CDocumentWebPage( pDoc )
{
}

CDocumentHomeWebPage::~CDocumentHomeWebPage()
{
}

void CDocumentHomeWebPage::GenerateBodyContent()
{
	InsertServerTable();

	InsertContent( _T( "&nbsp;" ) );

	InsertStatusTable();

	InsertContent( _T( "&nbsp;" ) );
	
	InsertLineCountTable();
	
	InsertContent( _T( "&nbsp;" ) );
	
	InsertExpectedVsObservedTable();
	
	InsertContent( _T( "&nbsp;" ) );
	
	InsertPayoutBreakdownTable();
	
	InsertContent( _T( "&nbsp;" ) );
	
	InsertConfidenceIntervalTable();

	BeginParagraph( haCenter );

	if ( GetResults().GetPOfCostly() == 0.0 )
	{
		InsertContent( _T( "Chance the game pays out more than it takes in:&nbsp;&nbsp;too&nbsp;small&nbsp;to&nbsp;measure" ) );
	}
	else
	{
		InsertContent( _T( "Chance the game pays out more than it takes in:&nbsp;&nbsp;1&nbsp;in&nbsp;" ) );

		InsertContent( 1.0 / GetResults().GetPOfCostly(), 0 );
	}

	EndParagraph();
}

void CDocumentHomeWebPage::InsertServerTable()
{
	BeginTable( haCenter );
	
		BeginRow();

			BeginColumn();

				InsertContent( _T( "Slot Server:&nbsp;&nbsp;" ) );

				InsertContent( GetDocument().GetSlotServerName() );

			EndColumn();

			BeginColumn( 50 );

			EndColumn();

			BeginColumn();

				InsertContent( _T( "Casino Base Port:&nbsp;&nbsp;" ) );

				InsertContent( GetDocument().GetSlotServerPort() - 2063 );

			EndColumn();

		EndRow();

	EndTable();
}

void CDocumentHomeWebPage::InsertStatusTable()
{
	BeginTable( haCenter );
	
		BeginRow();

			BeginColumn();

				InsertContent( _T( "Status:&nbsp;&nbsp;" ) );

				CAUSlotsTesterDoc::SPIN_STATE ss = GetDocument().GetSpinState();

				if ( ss == CAUSlotsTesterDoc::ssSpinning )
				{
					InsertContent( _T( "Spinning" ) );
				}
				else if ( ss == CAUSlotsTesterDoc::ssStopping )
				{
					InsertContent( _T( "Stopping" ) );
				}
				else
				{
					ASSERT( ss == CAUSlotsTesterDoc::ssStopped );
					InsertContent( _T( "Stopped" ) );
				}


			EndColumn();

			BeginColumn( 50 );

			EndColumn();

			BeginColumn();

				InsertContent( _T( "Total Bet:&nbsp;&nbsp;" ) );

				InsertContent( GetResults().GetTotalBet() );

			EndColumn();

			BeginColumn( 50 );

			EndColumn();

			BeginColumn();

				InsertContent( _T( "Bet Per Line:&nbsp;&nbsp;" ) );

				InsertContent( GetDocument().GetBetPerLine() );

			EndColumn();

			BeginColumn( 50 );

			EndColumn();

			BeginColumn();

				InsertContent( _T( "Lines Per Spin:&nbsp;&nbsp;" ) );

				InsertContent( GetDocument().GetLinesPerSpin() );

			EndColumn();

		EndRow();

	EndTable();
}

void CDocumentHomeWebPage::InsertLineCountTable()
{
	BeginTable( haCenter );
	
		BeginRow();

			BeginColumn( haCenter, 3 );

				BeginBold();

					InsertContent( _T( "Line Count" ) );

				EndBold();

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn( 3, 3 );

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn();

				InsertContent( _T( "Paid Lines Played" ) );

			EndColumn();

			BeginColumn( 10 );

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetPaidLinesPlayed() );

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn();

				InsertContent( _T( "Free Lines Played" ) );

			EndColumn();

			BeginColumn( 10 );

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetFreeLinesPlayed() );

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn( 3, 3 );

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn();

				InsertContent( _T( "Total Lines Played" ) );

			EndColumn();

			BeginColumn( 10 );

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetTotalLinesPlayed() );

			EndColumn();

		EndRow();

	EndTable();
}

void CDocumentHomeWebPage::InsertExpectedVsObservedTable()
{
	COLORREF crDifferenceText = crBlack;

	CSpinTestResults::PAYOUT_WITHIN_RANGE pwr = GetResults().IsPayoutWithinExpectations();

	if ( pwr == CSpinTestResults::pwrNo )
	{
		crDifferenceText = RGB( 0xFF, 0x00, 0x00 );
	}
	else if ( pwr == CSpinTestResults::pwrWarning )
	{
		crDifferenceText = RGB( 0xFF, 0x80, 0x00 );
	}

	BeginTable( haCenter );

		BeginRow();

			BeginColumn();

			EndColumn();

			BeginColumn( 50 );

			EndColumn();

			BeginColumn( haCenter );

				BeginBold();
				
					InsertContent( _T( "Expected" ) );

				EndBold();

			EndColumn();

			BeginColumn( 10 );

			EndColumn();

			BeginColumn();
		
				BeginBold();

				InsertContent( _T( "vs." ) );

				EndBold();

			EndColumn();

			BeginColumn( 10 );

			EndColumn();

			BeginColumn( haCenter );

				BeginBold();

					InsertContent( _T( "Observed" ) );

				EndBold();

			EndColumn();

			BeginColumn( 50 );

			EndColumn();

			BeginColumn();

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn( 3, 9 );

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn();

				InsertContent( _T( "Total Payout" ) );

			EndColumn();

			BeginColumn();

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetExpectedTotalPayout() );

			EndColumn();

			BeginColumn();

			EndColumn();
			
			BeginColumn();

			EndColumn();
			
			BeginColumn();

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetTotalPayout() );

			EndColumn();

			BeginColumn();

			EndColumn();
			
			BeginColumn( haRight );

				BeginFont( crDifferenceText );

					InsertContent( GetResults().GetTotalPayout() - GetResults().GetExpectedTotalPayout() );

				EndFont();

			EndColumn();
		
		EndRow();

		BeginRow();

			BeginColumn();

				InsertContent( _T( "Total Payout %" ) );
				
			EndColumn();

			BeginColumn();
				
			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetExpectedTotalPayoutPercent(), 3 );
				InsertContent( _T( "%" ) );

			EndColumn();

			BeginColumn();
				
			EndColumn();

			BeginColumn();
				
			EndColumn();

			BeginColumn();
				
			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetTotalPayoutPercent(), 3 );
				InsertContent( _T( "%" ) );
				
			EndColumn();

			BeginColumn();
				
			EndColumn();

			BeginColumn( haRight );

				BeginFont( crDifferenceText );

					InsertContent( GetResults().GetTotalPayoutPercent() - GetResults().GetExpectedTotalPayoutPercent(), 3 );
					InsertContent( _T( "%" ) );

				EndFont();

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn( 3, 9 );

			EndColumn();
		
		EndRow();

	EndTable();
}

void CDocumentHomeWebPage::InsertPayoutBreakdownTable()
{
	BeginTable( haCenter );

		BeginRow();

			BeginColumn( haCenter, 5 );

				BeginBold();

					InsertContent( _T( "Observed Payout Breakdown" ) );

				EndBold();

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn( 3, 5 );

			EndColumn();

		EndRow();

		for ( int n = 0; n < GetResults().GetNumGameModes(); n++ )
		{
			BeginRow();

				BeginColumn();

					BeginAnchor( GetDocument().GetGameModeURL( n ) );

						InsertContent( GetDocument().GetGameModeName( n ) );

					EndAnchor();

				EndColumn();

				BeginColumn( 50 );

				EndColumn();

				BeginColumn( haRight );

					InsertContent( GetResults().GetGameModePayout( n ) );

				EndColumn();

				BeginColumn( 20 );

				EndColumn();

				BeginColumn( haRight );

					InsertContent( GetResults().GetGameModePayoutPercent( n ), 3 );
					InsertContent( _T( "%" ) );

				EndColumn();

			EndRow();
		}

		for (int n = 0; n < GetResults().GetNumBonusGameTypes(); n++ )
		{
			BeginRow();

				BeginColumn();

					InsertContent( GetDocument().GetBonusGameTypeName( n ) );

				EndColumn();

				BeginColumn( 50 );

				EndColumn();

				BeginColumn( haRight );

					InsertContent( GetResults().GetBonusGameTypePayout( n ) );

				EndColumn();

				BeginColumn( 20 );

				EndColumn();

				BeginColumn( haRight );

					InsertContent( GetResults().GetBonusGameTypePayoutPercent( n ), 3 );
					InsertContent( _T( "%" ) );

				EndColumn();

			EndRow();
		}

		BeginRow();

			BeginColumn( 3, 5 );

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn();

				InsertContent( _T( "Subtotal Without Progressive" ) );

			EndColumn();

			BeginColumn( 50 );

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetTotalPayout() - GetResults().GetProgressivePayout() );

			EndColumn();

			BeginColumn( 20 );

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetTotalPayoutPercent() - GetResults().GetProgressivePayoutPercent(), 3 );
				InsertContent( _T( "%" ) );

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn();

				InsertContent( _T( "Progressive Jackpot" ) );

			EndColumn();

			BeginColumn( 50 );

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetProgressivePayout() );

			EndColumn();

			BeginColumn( 20 );

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetProgressivePayoutPercent(), 3 );
				InsertContent( _T( "%" ) );

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn( 3, 5 );

			EndColumn();

		EndRow();

		BeginRow();

			BeginColumn();

				InsertContent( _T( "Total Payout" ) );

			EndColumn();

			BeginColumn( 50 );

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetTotalPayout() );

			EndColumn();

			BeginColumn( 20 );

			EndColumn();

			BeginColumn( haRight );

				InsertContent( GetResults().GetTotalPayoutPercent(), 3 );
				InsertContent( _T( "%" ) );

			EndColumn();

		EndRow();

	EndTable();
}

void CDocumentHomeWebPage::InsertConfidenceIntervalTable()
{
	BeginTable( haCenter );
		
		BeginRow();

			BeginColumn();

				BeginBold();

					InsertContent( _T( "Confidence Interval" ) );

				EndBold();

			EndColumn();

			BeginColumn( 20 );

			EndColumn();

			BeginColumn();

				BeginBold();

					InsertContent( _T( "Minimum Payout" ) );

				EndBold();

			EndColumn();

			BeginColumn( 20 );

			EndColumn();

			BeginColumn();

				BeginBold();

					InsertContent( _T( "Maximum Payout" ) );

				EndBold();

			EndColumn();

		EndRow();

		BeginRow();
		
			BeginColumn( 3, 5 );

			EndColumn();

		EndRow();

		CSpinTestResults::CConfidenceIntervalArray a;

		GetResults().GetConfidenceIntervals( a );

		for ( int n = 0; n < a.GetSize(); n++ )
		{
			BeginRow();

				BeginColumn();

					InsertContent( a[ n ].m_dConfidenceLevel, 3 );
					InsertContent( _T( "%" ) );

				EndColumn();

				BeginColumn();

				EndColumn();

				BeginColumn( haRight );

					InsertContent( a[ n ].m_dMinValue, 3 );
					InsertContent( _T( "%" ) );

				EndColumn();

				BeginColumn();

				EndColumn();

				BeginColumn( haRight );

					InsertContent( a[ n ].m_dMaxValue, 3 );
					InsertContent( _T( "%" ) );

				EndColumn();

			EndRow();
		}

		BeginRow();

			BeginColumn( 3, 5 );

			EndColumn();

		EndRow();

	EndTable();
}
